/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package drawimage;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.*;
import java.net.*;


/**
 *
 * @author shohdi
 */
public class DrawImage extends JFrame {

    private String Error = "";
    private Image image = null;
    private Toolkit tool = Toolkit.getDefaultToolkit();
    
    public DrawImage()
    {
        super("Draw Image");
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        
        
        init();
        
    }
    
    
    
    
    
    public void init()
    {
        setSize(640,480);
        
        getImage("Koala.jpg");
        
        
    }
    
    private void getImage(String fileName)
    {
        URL url = null;
        
        try
        {
            url = this.getClass().getResource(fileName);
            image = tool.getImage(url);
            Error = "";
            
        }
        catch(Exception e)
        {
            Error="خطأ في فتح الصورة " + e.getMessage();
            repaint();
        }
                
               
            
        
                
    }
    
    public void paint(Graphics g)
    {
        Graphics2D g2D = (Graphics2D)g;
        AffineTransform identity  = new  AffineTransform();
        g2D.setTransform(identity);
        
        g2D.setColor(Color.BLACK);
        g2D.fill(new Rectangle(0,0,getSize().width,getSize().height));
        
        g2D.setColor(Color.WHITE);
        g2D.drawString(Error,20,20);
        
        try
            
        {
        g2D.drawImage(image,0,40, this);
        }
        catch(Exception e)
        {
            Error="خطأ في فتح الصورة " + e.getMessage();
            g2D.drawString(Error,20,20);
        }
        
        
    }
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        new DrawImage();
    }
}
